﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using BusinessManagement.Economy;
using DBO.Utility;
using SOAPPlayerService.Data;
using BusinessManagement;
using System.Net.Mail;
using DBO;

namespace SOAPPlayerService
{
    public class SOAPPlayerService : ISOAPPlayerService
    {
        #region Session
        public RequestResponse OpenSession(PLICCredentials creds)
        {
            if (creds == null || creds.Login == "" || creds.Password == "")
                return new RequestResponse() { ErrorMessage = "Login and/or pass are missing." };

            Session sess = SessionContainer.ConnectUser(creds);

            if (sess == null)
                return new RequestResponse() { ErrorMessage = "Authentication error." };

            return new RequestResponse() { Token = sess.Token };
        }

        public bool RefreshSession(string token)
        {
            return SessionContainer.RefreshSession(token);
        }

        public void CloseSession(string token)
        {
            SessionContainer.CloseSession(token);
        }

        public RequestResponse CreateUser(string mail, PLICCredentials Creds, string race)
        {
            RequestResponse Resp = new RequestResponse();
            Logger.ConsoleLogger.Log("SOAPCreate user with : " + mail + ", " + Creds.Login + ", " + Creds.Password + ", " + race);
            try
            {
                if (BusinessManagement.AccountManager.CreateAccount(Creds, new MailAddress(mail), race))
                    Resp.Message = "User created";
                else
                    Resp.ErrorMessage = "Login (" + Creds.Login + ") or mail (" + mail + ") already in use.";
            }
            catch (FormatException)
            {
                Resp.ErrorMessage = "Invalid mail address format.";
            }
            return Resp;
        }

        public RequestResponse RegeneratePass(string login, string mail)
        {
            RequestResponse Resp = new RequestResponse();

            try
            {
                BusinessManagement.AccountManager.ChangePassword(login, new MailAddress(mail));
            }
            catch (Exception)
            {
                Resp.ErrorMessage = "Invalid mail address : " + mail;
            }

            return Resp;
        }
        #endregion

        public DBO.Economy.Resources GetPlayerResources(string token)
        {
            return PlayerEconomy.GetPlayerResources(token);
        }

        public bool BuySlot(string token, string type)
        {
            return PlayerEconomy.BuySlot(token, type);
        }

        public bool SellSlot(string token, string type)
        {
            return PlayerEconomy.SellSlot(token, type);
        }

        public int CountSlots(string token, string type)
        {
            return PlayerEconomy.CountSlots(token, type);
        }

        public Player GetPlayerInfo(string token)
        {
            return BusinessManagement.SessionContainer.GetPlayerInfo(token);
        }



        public string[] GetSquad(string token, int points)
        {
            return PlayerEconomy.GetSquad(token, points);
        }

        public void SetSquad(string token, string[] squad, int points)
        {
            PlayerEconomy.SetSquad(token, squad, points);
        }
    }
}
